package com.gaoyi.crm.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.gaoyi.crm.R;
import com.gaoyi.crm.adapter.base.BaseEntityAdapter;
import com.gaoyi.crm.adapter.base.BaseViewHolder;
import com.gaoyi.crm.databinding.RvScannedBinding;
import com.gaoyi.crm.db.entity.Category;
import com.gaoyi.crm.db.entity.ItemNoStore;
import com.gaoyi.crm.util.LanguageUtils;

public class ScannedAdapter extends BaseEntityAdapter<RvScannedBinding, ItemNoStore> {

    private boolean mShowCbx = false;

    public void setShowCbx(boolean showCbx) {
        this.mShowCbx = showCbx;
        notifyItemRangeChanged(0, getItemCount());
    }

    public ScannedAdapter(Context context) {
        super(context);
    }

    @Override
    protected void setBindingVariable(RvScannedBinding binding) {

    }

    @Override
    protected void onBindItem(RvScannedBinding binding, ItemNoStore item, int position) {
        binding.cbx.setVisibility(mShowCbx ? View.VISIBLE : View.INVISIBLE);
        binding.tvItemNo.setText(mContext.getString(R.string.num_title, position + 1, item.getItemNo()));
        if (TextUtils.isEmpty(item.getCategoryName())) {
            Category category = item.getCategory();
            if (category != null) {
                binding.tvCategory.setText(LanguageUtils.getInstance().isEnglish() ? category.getEnName() : category.getCnName());
            }
        } else {
            binding.tvCategory.setText(item.getCategoryName());
        }
    }

    @Override
    protected void onCreateView(RvScannedBinding binding, BaseViewHolder holder) {
        binding.cbx.setOnCheckedChangeListener((compoundButton, b) -> getCurrentList().get(holder.getLayoutPosition()).setCheck(b));
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.rv_scanned;
    }
}
